package utils;

import com.fasterxml.jackson.databind.JsonNode;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 项目常用方法工具类集合
 * @author:LG
 * @date:Created at 2019/03/09
 */
public class JsonObjectUtils {




public static String[] mapConvertToArrayMap(Map<String,Object> map) {
        Collection<Object> valueCollection = map.values();
        final int size = valueCollection.size();

        List<Object> valueList = new ArrayList<Object>(valueCollection);

        String[] valueArray = new String[size];
        map.values().toArray(valueArray);
        return valueArray;

    }


    /**
     * json数据中取数据到JAVA对象
     * @param jsonNode : json值，
     * @param obj : 拷贝值对象
     * @return
     */
    public static void copyJsonToObject(JsonNode jsonNode ,Object obj){
        Class cls = obj.getClass();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field fd:declaredFields) {
            String fdName = fd.getName();
            JsonNode jsonNodeTemp = jsonNode.get(fdName);
            if(jsonNodeTemp==null){
                continue;
            }
           if(jsonNodeTemp.getNodeType()==null){
               fd.setAccessible(true);
               try {
                   fd.set(obj,null);
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               }
           }else{
               fd.setAccessible(true);
               try {
                  if(fd.getType().getName().equals("java.util.List")){
                      continue;
                  }else {
                      fd.set(obj, getRightValue(fd.getType().getName(), jsonNodeTemp.toString()));
                  }
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               }
           }
        }
    }

    /**
     * 判断类型进行转换
     * @param clsName
     * @param val
     * @return
     */
    public static Object getRightValue(String clsName,String val){
        Object obj = null;
        String cls = clsName;
    switch(clsName){
        case "java.lang.Float": obj = Float.valueOf(val);break;
        case "float": obj = Float.valueOf(val);break;
        case "java.lang.Integer":obj = Integer.parseInt(val.replaceAll("\"",""));break;
        case "int":obj = Integer.parseInt(val.replaceAll("\"","").replaceAll("'",""));break;
        case "java.sql.Timestamp":obj = new Timestamp(Long.valueOf(val));break;
        case "java.lang.Double":obj = Double.valueOf(val);break;
        case "double":obj = Double.valueOf(val);break;
        case "java.lang.Boolean":obj = Boolean.valueOf(val);break;
        case "boolean":obj = Boolean.valueOf(val);break;
        default:obj = val.replaceAll("\"","").replaceAll("'","");
        }
        return obj;
    }


    /**
     *
     * 从requestmap中取出json数组对象字符串
     */

    public static String getStringFromRequestMap(Map<String, String[]> map){
        String nodeTree = null;
        for (Map.Entry<String, String[]> entry : map.entrySet()) {
            nodeTree = entry.getKey();
            if (nodeTree != null) {
                break;
            }
        }
        return nodeTree;
    }



    /**
     *
     * 从转换集合为数组
     */
    public static Object[] changeArrayFromList(List<Object> list){
        Object[] parmas = new Object[list.size()];
        for(int i = 0 ;i<list.size();i++){
            parmas[i] = list.get(i);
        }
        return parmas;
    }




}
